package com.simple.microservices.commons.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 从common模块复制过来，用于生成接口文档
 * 统一处理response
 */
public class Response<T> implements Serializable {

    /**
     * 错误提示信息
     */
    private String message = "".intern();

    /**
     * 错误的参数代码
     */
    private String code;


    /**
     * 错误的参数代码
     */
    private Boolean result;


    /**
     * 存储的数据
     */
    private T data;


    public String getMessage() {
        return this.message;
    }

    public String getCode() {
        return this.code;
    }

    public T getData() {
        return this.data;
    }

    public Boolean getResult() {
        return this.result;
    }

    //服务器获取验证码
    private static final String SUCCESS = "200";
    private static final String FAILED = "400";
    private static final Boolean YES = Boolean.TRUE;
    private static final Boolean NO = Boolean.FALSE;


    /**
     * 返回成功标识
     *
     * @param data 需要返回的数据
     * @param <E>  返回给前端的数据
     * @return
     */
    public static <E> Response success(E data) {
        Response responseData = new Response();
        responseData.data = data;
        responseData.code = SUCCESS;
        responseData.result = YES;
        return responseData;

    }


    /**
     * 返回成功
     *
     * @param <E>
     * @return
     */
    public static <E> Response success() {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = SUCCESS;
        responseData.result = YES;
        return responseData;

    }

    /**
     * 返回成功信息
     *
     * @param message
     * @param <E>
     * @return
     */
    public static <E> Response successMsg(String message) {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = SUCCESS;
        responseData.message = message;
        responseData.result = YES;
        return responseData;
    }

    /**
     * 返回成功消息和数据
     *
     * @param data
     * @param message
     * @param <E>
     * @return
     */
    public static <E> Response successDataMsg(E data, String message) {
        Response responseData = new Response();
        responseData.data = data;
        responseData.code = SUCCESS;
        responseData.message = message;
        responseData.result = YES;
        return responseData;
    }

    /**
     * 返回成功 列表
     *
     * @param data
     * @param <E>
     * @return
     */
    public static <E> Response arraySuccess(E data) {
        Response responseData = new Response();
        List<E> list = new ArrayList<E>();
        list.add(data);
        responseData.data = list;
        responseData.code = SUCCESS;
        responseData.result = YES;
        return responseData;

    }


    /**
     * 返回成功标识
     *
     * @param data 需要返回的数据
     * @param code 需要返回的状态
     * @param <E>  返回给前端的数据
     * @return
     */
    public static <E> Response success(String code, E data) {
        Response responseData = new Response();
        responseData.data = data;
        responseData.code = code;
        responseData.result = YES;
        return responseData;

    }


    /**
     * 返回成功标识
     *
     * @return
     */
    public static <E> Response successByArray() {
        Response responseData = new Response();
        responseData.data = new ArrayList<>();
        responseData.code = SUCCESS;
        responseData.result = YES;
        return responseData;

    }


    /**
     * 返回成功标识
     *
     * @return
     */
    public static <E> Response successByMap(Map<String, Object> map) {
        Response responseData = new Response();
        responseData.data = map;
        responseData.code = SUCCESS;
        responseData.result = YES;
        return responseData;

    }

    /**
     * 返回失败的标识
     *
     * @param data 需要返回的数据
     * @return
     */
    public static <E> Response error(E data) {
        Response responseData = new Response();
        responseData.data = data;
        responseData.code = FAILED;
        responseData.result = NO;
        return responseData;

    }


    /**
     * 返回失败的标识
     *
     * @param code 需要返回的code
     * @param data 需要返回的数据
     * @return
     */
    public static <E> Response error(String code, E data) {
        Response responseData = new Response();
        responseData.data = data;
        responseData.code = code;
        responseData.result = NO;
        return responseData;

    }


    /**
     * 返回失败的标识
     *
     * @return
     */
    public static <E> Response errorByArray() {

        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = FAILED;
        responseData.result = NO;
        return responseData;

    }

    /**
     * 返回自定义的标识
     *
     * @return
     */
    public static Response response(String code, Boolean result) {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = code;
        responseData.result = result;
        return responseData;

    }

    /**
     * 返回自定义的标识
     *
     * @return
     */
    public static <E> Response response(String code, E data) {
        Response responseData = new Response();
        responseData.data = data;
        responseData.code = code;
        return responseData;

    }

    /**
     * 返回失败的标识
     *
     * @return
     */
    public static <E> Response errorByMap() {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = FAILED;
        responseData.result = NO;
        return responseData;

    }

    /**
     * 返回错误
     *
     * @param <E>
     * @return
     */
    public static <E> Response error() {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = FAILED;
        responseData.result = NO;
        return responseData;

    }

    /**
     * 错误提示
     *
     * @param message 错误信息
     * @param <E>
     * @return
     */
    public static <E> Response errorMsg(String message) {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = FAILED;
        responseData.message = message;
        responseData.result = NO;
        return responseData;
    }

    /**
     * 错误提示
     *
     * @param message 错误信息
     * @param <E>
     * @return
     */
    public static <E> Response errorCodeMsg(String code, String message) {
        Response responseData = new Response();
        responseData.data = new JSONObject();
        responseData.code = code;
        responseData.message = message;
        responseData.result = NO;
        return responseData;
    }


//
//    public static class ResponseData<T> extends Response {
//
//        /**
//         * 错误提示信息
//         */
//        private String message = "".intern();
//
//        /**
//         * 错误的参数代码
//         */
//        private String code;
//
//
//        /**
//         * 错误的参数代码
//         */
//        private Boolean result;
//
//
//        /**
//         * 存储的数据
//         */
//        private T data;
//
//
//        public String getMessage() {
//            return message;
//        }
//
//        public String getCode() {
//            return code;
//        }
//
//        public T getData() {
//            return data;
//        }
//
//        public Boolean getResult() {
//            return result;
//        }
//
//        private ResponseData() {
//
//        }
//    }

    public Response() {

    }


    public Object readResolve() {
        return this;
    }

}
